package Model;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JTable;

import Controller.DB;

public class Order {
	private Map<MenuItem, Integer> lineItemMap;
	private Date date;
	private int orderID;
	
	public Order() {
		try {
			orderID = DB.getNextOrderID();
			date = new Date(System.currentTimeMillis());
			lineItemMap= new TreeMap<MenuItem, Integer>();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Order(int orderID, Date orderDate) {
		this.orderID = orderID;
		date = orderDate;
		lineItemMap= new TreeMap<MenuItem, Integer>();
		init();
	}
	
	public Order(int orderID, Date date, Map<MenuItem, Integer> map) {
		this.orderID = orderID;
		this.date = date;
		lineItemMap = map;
	}
	
	public void addLine(MenuItem itm, Integer qty) {
		lineItemMap.put(itm, qty);
	}
	
	public int getID() {
		return orderID;
	}
	
	public void setID(int id) {
		orderID = id;
	}
	
	public Date getDate() {
		return date;
	}
	
	public Map<MenuItem, Integer> getLineItems() {
		return lineItemMap;
	}
	
	public boolean hasItems() {
		Iterator<?> it = lineItemMap.entrySet().iterator();
		while(it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry pairs = (Map.Entry)it.next();
			int quantity = (int) pairs.getValue();
			if (quantity > 0) {
				return true;
			}
		}
		return false;
	}
	
	private void init() {
		String sql;
		ResultSet rs;
		try {
			Statement s = DB.conn.createStatement();
			sql = "SELECT orderDate FROM _445team11.Order WHERE orderID = '" + orderID + "'";
			rs = s.executeQuery(sql);
			rs.next();
			this.date = rs.getDate("orderDate");
			
			Statement t = DB.conn.createStatement();
			sql = "SELECT Item_itemID, itemQuantity FROM _445team11.OrderLine WHERE Order_orderID = '" + orderID + "'";
			rs = t.executeQuery(sql);
			
			while (rs.next()) {
				int itm = rs.getInt("Item_itemID");
				int qty = rs.getInt("itemQuantity");
				sql = "SELECT name, description, price FROM _445team11.Item WHERE itemID ='" + itm + "'";
				Statement r = DB.conn.createStatement();
				ResultSet rt = r.executeQuery(sql);
				while (rt.next()) {
					lineItemMap.put(new MenuItem(itm, rt.getString("name"), 
							rt.getString("description"), rt.getDouble("price")), qty);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public String getTotalString() {
		double total = 0;
		for (MenuItem itm : lineItemMap.keySet()) {
			total += lineItemMap.get(itm) * itm.getPrice();
		}
		return String.format("Total: $%6.2f\n",  total);
	}
	public JTable createOrderTable() {
		Object[] headers = {"ItemID", "Name", "Price", "Quantity", "Extended Price"};
		Object[][] data = new Object[lineItemMap.size()][5];

		Iterator<MenuItem> itmItr = lineItemMap.keySet().iterator();
		int i = 0;
		while (itmItr.hasNext()){
			MenuItem itm = itmItr.next();
			data[i][0] = itm.getItemID();
			data[i][1] = itm.getName();
			data[i][2] = itm.getPrice();
			int qty = lineItemMap.get(itm);
			data[i][3] = qty;
			data[i][4] = String.format("%4.2f", qty * itm.getPrice());
			i++;
		}
		JTable table = new JTable(data, headers);
		table.getColumnModel().getColumn(0).setWidth(12);
		table.getColumnModel().getColumn(1).setWidth(60);
		table.getColumnModel().getColumn(2).setWidth(8);
		table.getColumnModel().getColumn(3).setWidth(10);
		table.getColumnModel().getColumn(4).setWidth(15);
		return table;
	}

}